﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace JYStockAnalyser.WinUI
{
    public class DictionaryXmlSerializer
    {
        public static void Serialize(TextWriter writer, IDictionary dictionary) 
        { 
            List<Entry> entries = new List<Entry>(dictionary.Count); 
            foreach (object key in dictionary.Keys) 
            { 
                entries.Add(new Entry(key, dictionary[key])); 
            } 
            XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>)); 
            serializer.Serialize(writer, entries); 
        }        
        public static void Deserialize(TextReader reader, IDictionary dictionary) 
        { 
            dictionary.Clear(); 
            XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
            try
            {
                List<Entry> list = (List<Entry>)serializer.Deserialize(reader);
                foreach (Entry entry in list)
                {
                    dictionary.Add(entry.Key, entry.Value);
                } 
            }
            catch (System.InvalidOperationException)
            {
                
            }
          
        }      
    }

    public class Entry 
    { 
        public object Key; 
        public object Value; 
        public Entry() 
        { 
        } 
        public Entry(object key, object value) 
        { 
            Key = key; 
            Value = value; 
        } 
    }
}
